package com.isunland.managesystem.utils;

import android.content.Context;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.isunland.managesystem.entity.CurrentUser;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/7/10 11:16
 */

public class LocationUtil {
  @SuppressWarnings("unused") private static LocationUtil sLocationUtil;
  private static AMapLocationClient mLocationClient;
  private Context mContext;

  private LocationUtil(final Context context) {
    this.mContext = context;
    //每次新建的时候销毁上次的locationClient
    destroy();
    mLocationClient = initLocation(context);
    //设置定位回调监听
    setSimpleLocationListener(null);
  }

  public static LocationUtil newInstance(Context context) {

    return new LocationUtil(context);
  }

  private AMapLocationClient initLocation(final Context context) {
    //初始化定位
    AMapLocationClient locationClient = new AMapLocationClient(context);
    //初始化定位参数
    AMapLocationClientOption locationOption = new AMapLocationClientOption();
    //设置定位模式
    locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
    //单次定位
    locationOption.setOnceLocation(true);
    //设置定位参数
    locationClient.setLocationOption(locationOption);
    // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除

    return locationClient;
  }

  public void startLocation() {
    if (mLocationClient != null) mLocationClient.startLocation();//启动定位
  }

  @SuppressWarnings("unused") public void stopLocation() {
    if (mLocationClient != null) mLocationClient.stopLocation();//关闭定位
  }

  @SuppressWarnings({ "unused", "WeakerAccess" }) public void destroy() {
    if (null != mLocationClient) {
      mLocationClient.onDestroy();
    }
  }

  @SuppressWarnings("unused")
  public LocationUtil setLocationListener(AMapLocationListener listener) {
    mLocationClient.setLocationListener(listener);
    return this;
  }

  public LocationUtil setSimpleLocationListener(final SimpleCustomLocationListener listener) {
    mLocationClient.setLocationListener(new AMapLocationListener() {
      @Override public void onLocationChanged(AMapLocation aMapLocation) {
        if ((aMapLocation == null) || (aMapLocation.getErrorCode() != 0)) {
          if (listener != null) listener.onLocationError();
          return;
        }
        //更新CurrentUser的位置信息,重新设置标签
        CurrentUser.newInstance(mContext).setProvince(aMapLocation.getProvince());
        CurrentUser.newInstance(mContext).setLongitude(String.valueOf(aMapLocation.getLongitude()));
        CurrentUser.newInstance(mContext).setLatitude(String.valueOf(aMapLocation.getLatitude()));
        CurrentUser.newInstance(mContext).setAddress(String.valueOf(aMapLocation.getAddress()));
        if (listener != null) listener.onLocationValid(aMapLocation);
        destroy();
      }
    });
    return this;
  }

  /** 自定义的回调方法,只有在位置有效时才会回调.已经做了放入currentUser,之后会执行销毁onDestroy.如果需要完全自定义,使用setListener */
  public interface SimpleCustomLocationListener {
    /** 自定义的回调方法,只有在位置有效时才会回调.已经做了放入currentUser,之后会执行销毁onDestroy.如果需要完全自定义,使用setListener */
    void onLocationValid(AMapLocation location);

    void onLocationError();
  }
}
